package com.xinqi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinqi.bean.User;
import com.xinqi.common.R;
import com.xinqi.mapper.UserMapper;
import com.xinqi.service.UserService;
import com.xinqi.util.MD5;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author xin麒
 * @date 2023/1/14 20:33
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public R<User> register(User user) {
        String mobile = user.getMobile();
        if (mobile == null) return R.error("mobile has not given!");
        //检查是否已经注册
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getMobile,mobile);
        User one = getOne(queryWrapper);
        if (one != null) return R.error("this mobile has been register!");
        //检查密码是否为空
        String password = user.getPassword();
        if (password == null) return R.error("password has not given!");
        //隐藏密码
        User userOne = new User();
        userOne.setMobile(mobile);
        userOne.setPassword(MD5.encrypt(password));
        save(userOne);

        return R.success(userOne);
    }
}
